package com.suda.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.suda.dao.AdminMapper;
import com.suda.pojo.Admin;
import com.suda.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Service
public class AdminServiceImpl implements AdminService {

    @Autowired
    private AdminMapper adminMapper;


    @Override
    public PageInfo<Admin> findPage(Admin admin, int page, int size) {
        PageHelper.startPage(page,size);
        //搜索条件构建
        Example example = createExample(admin);
        //执行搜索
        return new PageInfo<Admin>(adminMapper.selectByExample(example));
    }

    public Example createExample(Admin admin) {
        Example example=new Example(Admin.class);
        Example.Criteria criteria = example.createCriteria();
        if(admin!=null)
        {
            if(!StringUtils.isEmpty(admin.getUsername())){
                criteria.andEqualTo("username",admin.getUsername());
            }
        }
        return example;
    }

    @Override
    public PageInfo<Admin> findPage(int page, int size) {
        PageHelper.startPage(page,size);
        //分页查询
        return new PageInfo<Admin>(adminMapper.selectAll());
    }

    @Override
    public List<Admin> findList(Admin admin) {
        Example example=createExample(admin);
        return adminMapper.selectByExample(example);
    }

    @Override
    public void delete(String username) {
    adminMapper.deleteByPrimaryKey(username);
    }

    @Override
    public void update(Admin admin) {
   adminMapper.updateByPrimaryKey(admin);
    }

    @Override
    public void add(Admin admin) {
    adminMapper.insert(admin);
    }

    @Override
    public List<Admin> findAll() {
        return adminMapper.selectAll();
    }

    @Override
    public Admin findById(String username){
        return  adminMapper.selectByPrimaryKey(username);
    }
}
